/*
 * Copyright (c) 2011-2012 Tygron Serious Gaming
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'Swing to Nifty Converter' nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package nl.tygron.niftyconverter.converters;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import nl.tygron.niftyconverter.ImageDatabase;
import nl.tygron.niftyconverter.UIConverterDatabase;
import nl.tygron.niftyconverter.elements.NElement;
import nl.tygron.niftyconverter.elements.NImageElement;
import nl.tygron.niftyconverter.elements.NLabelElement;
import nl.tygron.niftyconverter.elements.NiftyStructure;

/**
 * @author Raymond Weijermars
 * @Specialism GUI
 * @Maintainer Jeroen Warmerdam
 */

public class LabelConverter extends ComponentConverter {

	private static final Logger logger = Logger.getLogger(LabelConverter.class.getName());

	public LabelConverter(ImageDatabase dbase) {
		super(dbase);
	}

	@Override
	protected NElement detailedDecorateNiftyComponent(Component comp, NiftyStructure nStructure,
	        UIConverterDatabase uiConverterDatabaseRef, Component topMostObject) {

		NElement nElement;

		JLabel jLabel = (JLabel) comp;
		ImageIcon icon = (ImageIcon) jLabel.getIcon();

		String imageFileName = "";

		String labelText = jLabel.getText();

		Font font = jLabel.getFont();
		Color fontColor = jLabel.getForeground();

		String align = this.getObjectAlignment(jLabel.getHorizontalAlignment());
		String valign = this.getObjectAlignment(jLabel.getVerticalAlignment());

		if ("".equals(labelText) && icon != null) { // this label is actually an image(backgrounds, temp images for graphs)
			imageFileName = this.getImageIconLocation(icon);

			if ("".equals(imageFileName)) {
				logger.severe(icon.getDescription() + " is not present in the image database.");
				return null;
			}

			if (imageFileName == null) {
				logger.severe(icon.getDescription() + " is not present in the image database.");
				return null;
			}

			if (icon.getIconWidth() < width || icon.getIconHeight() < height) {
				nElement = new NImageElement(id, x, y, width, height, icon.getIconWidth(), icon.getIconHeight(), nStructure, imageFileName,
				        align, valign, false);
			} else {
				nElement = new NImageElement(id, x, y, width, height, nStructure, imageFileName, align, valign, false);
			}
		} else if (!"".equals(labelText) && icon != null) { // this label is actually an image with a label ontop
			imageFileName = this.getImageIconLocation(icon);
			if ("".equals(imageFileName)) {

			}
			nElement = new NImageElement(id, x, y, width, height, nStructure, imageFileName, labelText, font, fontColor, align, valign,
			        false);
		} else { // finally an actual label with label functionality :P
			boolean bold = jLabel.getFont().isBold();
			if (jLabel.isOpaque()) {
				Color backgroundColor = jLabel.getBackground();
				nElement = new NLabelElement(id, x, y, width, height, nStructure, labelText, font, fontColor, backgroundColor, align,
				        valign, bold);
			} else {

				nElement = new NLabelElement(id, x, y, width, height, nStructure, labelText, font, fontColor, align, valign, bold);
			}
		}

		return nElement;
	}
}
